#include "mmsi_country.hpp"
#include <algorithm>
#include <vector>
#include <marnav/utils/mmsi.hpp>

namespace marnav
{
namespace utils
{
namespace
{
// clang-format off
static const std::vector<mmsi_country_info> country_list = {
	{ 201, "AL", "Albania" },
	{ 202, "AD", "Andorra" },
	{ 203, "AT", "Austria" },
	{ 204, "AZ", "Azores" },
	{ 205, "BE", "Belgium" },
	{ 206, "BY", "Belarus" },
	{ 207, "BG", "Bulgaria" },
	{ 208, "VA", "Vatican City State" },
	{ 209, "CY", "Cyprus" },
	{ 210, "CY", "Cyprus" },
	{ 211, "DE", "Germany" },
	{ 212, "CY", "Cyprus" },
	{ 213, "GE", "Georgia" },
	{ 214, "MD", "Moldova" },
	{ 215, "MT", "Malta" },
	{ 216, "AM", "Armenia" },
	{ 218, "DE", "Germany" },
	{ 219, "DK", "Denmark" },
	{ 220, "DK", "Denmark" },
	{ 224, "ES", "Spain" },
	{ 225, "ES", "Spain" },
	{ 226, "FR", "France" },
	{ 227, "FR", "France" },
	{ 228, "FR", "France" },
	{ 229, "MT", "Malta" },
	{ 230, "FI", "Finland" },
	{ 231, "FO", "Faroe Islands" },
	{ 232, "GB", "Great Britain" },
	{ 233, "GB", "Great Britain" },
	{ 234, "GB", "Great Britain" },
	{ 235, "GB", "Great Britain" },
	{ 236, "GI", "Gibraltar" },
	{ 237, "GR", "Greece" },
	{ 238, "HR", "Croatia" },
	{ 239, "GR", "Greece" },
	{ 240, "GR", "Greece" },
	{ 241, "GR", "Greece" },
	{ 242, "MA", "Morocco" },
	{ 243, "HU", "Hungary" },
	{ 244, "NL", "Netherlands" },
	{ 245, "NL", "Netherlands" },
	{ 246, "NL", "Netherlands" },
	{ 247, "IT", "Italy" },
	{ 248, "MT", "Malta" },
	{ 249, "MT", "Malta" },
	{ 250, "IE", "Ireland" },
	{ 251, "IS", "Iceland" },
	{ 252, "LI", "Liechtenstein" },
	{ 253, "LU", "Luxembourg" },
	{ 254, "MC", "Monaco" },
	{ 255, "PT", "Madeira" },
	{ 256, "MT", "Malta" },
	{ 257, "NO", "Norway" },
	{ 258, "NO", "Norway" },
	{ 259, "NO", "Norway" },
	{ 261, "PL", "Poland" },
	{ 262, "ME", "Montenegro" },
	{ 263, "PT", "Portugal" },
	{ 264, "RO", "Romania" },
	{ 265, "SE", "Sweden" },
	{ 266, "SE", "Sweden" },
	{ 267, "SK", "Slovak Republic" },
	{ 268, "SM", "San Marino" },
	{ 269, "CH", "Switzerland" },
	{ 270, "CZ", "Czech Republic" },
	{ 271, "TR", "Turkey" },
	{ 272, "UA", "Ukraine" },
	{ 273, "RU", "Russian" },
	{ 274, "MK", "Macedonia" },
	{ 275, "LV", "Latvia" },
	{ 276, "EE", "Estonia" },
	{ 277, "LT", "Lithuania" },
	{ 278, "SI", "Slovenia" },
	{ 279, "RS", "Serbia" },
	{ 301, "AI", "Anguilla" },
	{ 303, "AK", "Alaska" },
	{ 304, "AG", "Antigua and Barbuda" },
	{ 305, "AG", "Antigua and Barbuda" },
	{ 306, "AN", "Antilles" },
	{ 307, "AW", "Aruba" },
	{ 308, "BS", "Bahamas" },
	{ 309, "BS", "Bahamas" },
	{ 310, "BM", "Bermuda" },
	{ 311, "BS", "Bahamas" },
	{ 312, "BZ", "Belize" },
	{ 314, "BB", "Barbados" },
	{ 316, "CA", "Canada" },
	{ 319, "KY", "Cayman Islands" },
	{ 321, "CR", "Costa Rica" },
	{ 323, "CU", "Cuba" },
	{ 325, "DM", "Dominica" },
	{ 327, "DM", "Dominican Republic" },
	{ 329, "GP", "Guadeloupe" },
	{ 330, "GD", "Grenada" },
	{ 331, "GL", "Greenland" },
	{ 332, "GT", "Guatemala" },
	{ 334, "HN", "Honduras" },
	{ 336, "HT", "Haiti" },
	{ 338, "US", "United States of America" },
	{ 339, "JM", "Jamaica" },
	{ 341, "KN", "Saint Kitts and Nevis" },
	{ 343, "LC", "Saint Lucia" },
	{ 345, "MX", "Mexico" },
	{ 347, "MQ", "Martinique" },
	{ 348, "MS", "Montserrat" },
	{ 350, "NI", "Nicaragua" },
	{ 351, "PA", "Panama" },
	{ 352, "PA", "Panama" },
	{ 353, "PA", "Panama" },
	{ 354, "PA", "Panama" },
	{ 355, "PA", "Panama" },
	{ 356, "PA", "Panama" },
	{ 357, "PA", "Panama" },
	{ 358, "PR", "Puerto Rico" },
	{ 359, "SV", "El Salvador" },
	{ 361, "PM", "Saint Pierre and Miquelon" },
	{ 362, "TT", "Trinidad and Tobago" },
	{ 364, "TC", "Turks and Caicos Islands" },
	{ 366, "US", "United States of America" },
	{ 367, "US", "United States of America" },
	{ 368, "US", "United States of America" },
	{ 369, "US", "United States of America" },
	{ 370, "PA", "Panama" },
	{ 371, "PA", "Panama" },
	{ 372, "PA", "Panama" },
	{ 373, "PA", "Panama" },
	{ 374, "PA", "Panama" },
	{ 375, "VC", "Saint Vincent and the Grenadines" },
	{ 376, "VC", "Saint Vincent and the Grenadines" },
	{ 377, "VC", "Saint Vincent and the Grenadines" },
	{ 378, "VG", "British Virgin Islands" },
	{ 379, "AE", "United States Virgin Islands" },
	{ 401, "AF", "Afghanistan" },
	{ 403, "SA", "Saudi Arabia" },
	{ 405, "BD", "Bangladesh" },
	{ 408, "BH", "Bahrain" },
	{ 410, "BT", "Bhutan" },
	{ 412, "CN", "China" },
	{ 413, "CN", "China" },
	{ 414, "CN", "China" },
	{ 416, "TW", "Taiwan" },
	{ 417, "LK", "Sri Lanka" },
	{ 419, "IN", "India" },
	{ 422, "IR", "Iran" },
	{ 423, "AZ", "Azerbaijani Republic" },
	{ 425, "IQ", "Iraq" },
	{ 428, "IL", "Israel" },
	{ 431, "JP", "Japan" },
	{ 432, "JP", "Japan" },
	{ 434, "TM", "Turkmenistan" },
	{ 436, "KZ", "Kazakhstan" },
	{ 437, "UZ", "Uzbekistan" },
	{ 438, "JO", "Jordan" },
	{ 440, "KR", "Korea" },
	{ 441, "KR", "Korea" },
	{ 443, "PS", "Palestine" },
	{ 445, "KP", "People's Rep. of Korea" },
	{ 447, "KW", "Kuwait" },
	{ 450, "LB", "Lebanon" },
	{ 451, "KG", "Kyrgyz Republic" },
	{ 453, "MO", "Macao" },
	{ 455, "MV", "Maldives" },
	{ 457, "MN", "Mongolia" },
	{ 459, "NP", "Nepal" },
	{ 461, "OM", "Oman" },
	{ 463, "PK", "Pakistan" },
	{ 466, "QA", "Qatar" },
	{ 468, "SY", "Syrian Arab Republic" },
	{ 470, "AE", "United Arab Emirates" },
	{ 471, "AE", "United Arab Emirates" },
	{ 472, "TJ", "Tajikistan" },
	{ 473, "YE", "Yemen" },
	{ 475, "YE", "Yemen" },
	{ 477, "HK", "Hong Kong" },
	{ 478, "BA", "Bosnia and Herzegovina" },
	{ 501, "TF", "Adelie Land" },
	{ 503, "AU", "Australia" },
	{ 506, "MM", "Myanmar" },
	{ 508, "BN", "Brunei Darussalam" },
	{ 510, "FM", "Micronesia" },
	{ 511, "PW", "Palau" },
	{ 512, "NZ", "New Zealand" },
	{ 514, "KH", "Cambodia" },
	{ 515, "KH", "Cambodia" },
	{ 516, "CX", "Christmas Island" },
	{ 518, "CK", "Cook Islands" },
	{ 520, "FJ", "Fiji" },
	{ 523, "CC", "Cocos" },
	{ 525, "ID", "Indonesia" },
	{ 529, "KI", "Kiribati" },
	{ 531, "LA", "Lao People's Dem. Rep." },
	{ 533, "MY", "Malaysia" },
	{ 536, "MP", "Northern Mariana Islands" },
	{ 538, "MH", "Marshall Islands" },
	{ 540, "NC", "New Caledonia" },
	{ 542, "NU", "Niue" },
	{ 544, "NR", "Nauru" },
	{ 546, "PF", "French Polynesia" },
	{ 548, "PH", "Philippines" },
	{ 553, "PG", "Papua New Guinea" },
	{ 555, "PN", "Pitcairn Island" },
	{ 557, "SB", "Solomon Islands" },
	{ 559, "AS", "American Samoa" },
	{ 561, "WS", "Samoa" },
	{ 563, "SG", "Singapore" },
	{ 564, "SG", "Singapore" },
	{ 565, "SG", "Singapore" },
	{ 566, "SG", "Singapore" },
	{ 567, "TH", "Thailand" },
	{ 570, "TO", "Tonga" },
	{ 572, "TV", "Tuvalu" },
	{ 574, "VN", "Viet Nam" },
	{ 576, "VU", "Vanuatu" },
	{ 577, "VU", "Vanuatu" },
	{ 578, "WF", "Wallis and Futuna Islands" },
	{ 601, "ZA", "South Africa" },
	{ 603, "AO", "Angola" },
	{ 605, "DZ", "Algeria" },
	{ 607, "TF", "Saint Paul" },
	{ 608, "SH", "Ascension Island" },
	{ 609, "BI", "Burundi" },
	{ 610, "BJ", "Benin" },
	{ 611, "BW", "Botswana" },
	{ 612, "CF", "Central African Republic" },
	{ 613, "CM", "Cameroon" },
	{ 615, "CD", "Congo" },
	{ 616, "KM", "Comoros" },
	{ 617, "CV", "Cape Verde" },
	{ 618, "TF", "Crozet Archipelago" },
	{ 619, "CI", "Ivory Coast" },
	{ 620, "KM", "Comoros (Union of the)" },
	{ 621, "DJ", "Djibouti" },
	{ 622, "EG", "Egypt" },
	{ 624, "ET", "Ethiopia" },
	{ 625, "ER", "Eritrea" },
	{ 626, "GA", "Gabonese Republic" },
	{ 627, "GH", "Ghana" },
	{ 629, "GM", "Gambia" },
	{ 630, "GW", "Guinea-Bissau" },
	{ 631, "GQ", "Equatorial Guinea" },
	{ 632, "GN", "Guinea" },
	{ 633, "BF", "Burkina Faso" },
	{ 634, "KE", "Kenya" },
	{ 635, "TF", "Kerguelen Islands" },
	{ 636, "LR", "Liberia" },
	{ 637, "LR", "Liberia" },
	{ 638, "SS", "South Sudan (Republic of)" },
	{ 642, "LY", "Libya" },
	{ 644, "LS", "Lesotho" },
	{ 645, "MU", "Mauritius" },
	{ 647, "MG", "Madagascar" },
	{ 649, "ML", "Mali" },
	{ 650, "MZ", "Mozambique" },
	{ 654, "MR", "Mauritania" },
	{ 655, "MW", "Malawi" },
	{ 656, "NE", "Niger" },
	{ 657, "NG", "Nigeria" },
	{ 659, "NA", "Namibia" },
	{ 660, "RE", "Reunion" },
	{ 661, "RW", "Rwanda" },
	{ 662, "SD", "Sudan" },
	{ 663, "SN", "Senegal" },
	{ 664, "SC", "Seychelles" },
	{ 665, "SH", "Saint Helena" },
	{ 666, "SO", "Somali Democratic Republic" },
	{ 667, "SL", "Sierra Leone" },
	{ 668, "ST", "Sao Tome and Principe" },
	{ 669, "SZ", "Swaziland" },
	{ 670, "TD", "Chad" },
	{ 671, "TG", "Togolese Republic" },
	{ 672, "TN", "Tunisia" },
	{ 674, "TZ", "Tanzania" },
	{ 675, "UG", "Uganda" },
	{ 676, "CD", "Dem Rep.of the Congo" },
	{ 677, "TZ", "Tanzania" },
	{ 678, "ZM", "Zambia" },
	{ 679, "ZW", "Zimbabwe" },
	{ 701, "AR", "Argentine Republic" },
	{ 710, "BR", "Brazil" },
	{ 720, "BO", "Bolivia" },
	{ 725, "CL", "Chile" },
	{ 730, "CO", "Colombia" },
	{ 735, "EC", "Ecuador" },
	{ 740, "FK", "Falkland Islands" },
	{ 745, "GY", "Guiana" },
	{ 750, "GY", "Guyana" },
	{ 755, "PY", "Paraguay" },
	{ 760, "PE", "Peru" },
	{ 765, "SR", "Suriname" },
	{ 770, "UY", "Uruguay" },
	{ 775, "VE", "Venezuela" },
};
// clang-format on
}

/// Returns country information for the specified MMSI.
///
/// If unknown, strings will be empty.
mmsi_country_info mmsi_country(const mmsi & m)
{
	const auto i = std::find_if(
		std::begin(country_list), std::end(country_list), [m](const mmsi_country_info & info) {
			return (m.is_regular() && m.regular_mid() == info.mid)
				|| (m.is_sar_aircraft() && m.sar_mid() == info.mid);
		});

	if (i == std::end(country_list))
		return {mmsi::initial_value, "", ""};

	return *i;
}
}
}
